﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class Admin_Job_Job_Manager : System.Web.UI.Page
{
    JobManager dllJobManager = new JobManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();
    Authentication dllAuthentication = new Authentication();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isAdminLogin();
        this.btnAdd.Enabled = false;
        this.btnAddJob.Enabled = false;
        this.btnDel.Enabled = false;
        _powerTable = dllAuthentication.getPower(objUserInfo.UserName, "岗位管理");
        for (int i = 0; i < _powerTable.Rows.Count; i++)
        {
            if (_powerTable.Rows[i]["PowerName"].ToString() == "新建")
            {
                this.btnAdd.Enabled = true;
                this.btnAddJob.Enabled = true;
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "删除")
            {
                this.btnDel.Enabled = true;
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "查看")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "修改")
            {
            }
        } 
        if (!IsPostBack)
        {
            
            BindGridView();
            
        }
        System.Web.HttpContext.Current.Response.Cookies["scrolltop"].Expires = DateTime.Now.AddDays(-1); 
    }


    /// <summary>
    /// 绑定GridView
    /// </summary>
    void BindGridView()
    {
        this.gdvJob.DataSource = dllJobManager.get();
        this.gdvJob.DataBind();
    }

    


    /// <summary>
    /// 添加岗位事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2008-12-31</date>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DataTable _job = new DataTable();
        if (this.hId.Value.Length != 0)
        {
            object[] _value1 = new object[1];
            _value1[0] = Convert.ToInt32(this.hId.Value);
            _job = dllJobManager.get(JobManager.field.JobId.ToString(), _value1);
        }        
        if (_job.Rows.Count==0)
        {
            object[] _value2 = new object[2];
            _value2[0] = this.txtJobName.Text;
            _value2[1] = this.txtJobRemark.Text;
            int result = dllJobManager.add(_value2);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加岗位未成功，请重试！");
            }
            else
            {
                Response.Redirect("Job_Manager.aspx");
            }
        }
        else
        {
            object[] _value2 = new object[3];
            _value2[0] = Convert.ToInt32(this.hId.Value);
            _value2[1] = this.txtJobName.Text;
            _value2[2] = this.txtJobRemark.Text;
            int result = dllJobManager.edit(JobManager.field.JobId.ToString(),_value2);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("更新岗位未成功，请重试！");
            }
            else
            {
                Response.Redirect("Job_Manager.aspx");
            }
        }
    }

    /// <summary>
    /// 删除事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2008-12-31</date>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.gdvJob.Rows[i].Cells[1].Text);

        int result = dllJobManager.del(JobManager.field.JobId.ToString(), _value);
        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("删除岗位未成功，请重试！");
        }
        else
        {
            Response.Redirect("Job_Manager.aspx");
        }
    }


    /// <summary>
    /// 分页方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvJob_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gdvJob.PageIndex = e.NewPageIndex;
        BindGridView();
    }


    /// <summary>
    /// GridView选择行事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvJob_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.hId.Value = this.gdvJob.DataKeys[e.NewSelectedIndex]["JobId"].ToString();
        this.txtJobName.Text = this.gdvJob.Rows[e.NewSelectedIndex].Cells[2].Text;
        this.txtJobRemark.Text = this.gdvJob.Rows[e.NewSelectedIndex].Cells[3].Text;
        if (this.txtJobRemark.Text == "&nbsp;")
        {
            this.txtJobRemark.Text = "";
        }

    }

    /// <summary>
    /// GridView行创建事件，同时修改鼠标经过颜色和绑定SelectedIndexChanging事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvJob_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标进入行时
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
            //鼠标离开行时
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            //任意行都为选择行
            e.Row.Attributes.Add("onclick", "__doPostBack('gdvJob','Select$" + e.Row.RowIndex + "')");
        }
    }


    /// <summary>
    /// 新建岗位事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddJob_Click(object sender, EventArgs e)
    {
        this.hId.Value = "0";
        this.txtJobName.Text = "";
        this.txtJobRemark.Text = "";
    }


    /// <summary>
    /// 删除事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-25</date>
    protected void btnDel_Click1(object sender, EventArgs e)
    {
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        for (int i = 0; i < this.gdvJob.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvJob.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                object[] _value = new object[1];
                _value[0] = Convert.ToInt32(this.gdvJob.DataKeys[i]["JobId"].ToString());
                _valueList.Add(_value);
                _count++;
            }
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请至少选择一个岗位", Page);
            return;
        }
        int result = dllJobManager.delWithTransaction(JobManager.field.JobId.ToString(), _valueList);
        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("删除岗位未成功，请重试！");
        }
        else
        {
            Response.Redirect("Job_Manager.aspx");
        }

    }
    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvJob.Rows.Count; i++)
            {
                CheckBox ckb = (CheckBox)this.gdvJob.Rows[i].Cells[0].FindControl("ckbSelect");
                ckb.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvJob.Rows.Count; i++)
            {
                CheckBox ckb = (CheckBox)this.gdvJob.Rows[i].Cells[0].FindControl("ckbSelect");
                ckb.Checked = false;
            }
        }
    }
}
